package br.unifor.mia.graph.setorcensitario;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

@Entity
public class Link {

	/**
	 * Name of link
	 */
	@Id
	private String name;
	
	/**
	 * From Node #
	 */
	@ManyToOne(optional = false)
	private Node user;
	
	/**
	 * To Node #
	 */
	@ManyToOne(optional = false)
	private Node sector;
	
	/**
	 * Value of link
	 */
	@OneToMany(mappedBy = "link")
	@LazyCollection(LazyCollectionOption.EXTRA)
	private Set<Crime> value = new HashSet<Crime>();

	/**
	 * Default Constructor, used by Hibernate.
	 */
	public Link() {
		// Default Constructor, used by Hibernate.
	}

	public Link(String name, Node userNode, Node sectorNode) {
		this.name = name;
		this.user = userNode;
		this.sector = sectorNode;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	protected void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the tail
	 */
	public Node getUser() {
		return user;
	}

	protected void setUser(Node userNode) {
		this.user = userNode;
	}
	
	/**
	 * @return the head
	 */
	public Node getSector() {
		return sector;
	}

	protected void setSector(Node sectorNode) {
		this.sector = sectorNode;
	}
	
	/**
	 * @return the value
	 */
	public Set<Crime> getValue() {
		return value;
	}
	
	public void addCrime(Crime momento) {
		value.add(momento);
	}

	public void addCrime(Set<Crime> momentos) {
		value.addAll(momentos);
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof Link)) {
			return false;
		}
		Link other = (Link) obj;
		if (name == null) {
			if (other.name != null) {
				return false;
			}
		} else if (!name.equals(other.name)) {
			return false;
		}
		return true;
	}

	@Override
	public String toString() {
		return "Link [" + name + "]";
	}
}